# coding=utf-8
import requests
import re
import time
import  lxml.html as H
import base64
from fontTools.ttLib import TTFont
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup 

def get_data(url):
    chrome_options = Options()
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('–blink-settings=imagesEnabled=false')
    chrome_options.add_argument('User-Agen =  "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36"')
    chrome_options.add_argument('referer= "https://km.58.com/kmgaoxin/zufang/j1/?PGTID=0d300008-0021-fbf5-3f54-773bfb0fecb0&ClickID=2"')
    browser = webdriver.Chrome(chrome_options=chrome_options)                   
    browser.get("https://km.58.com/kmgaoxin/zufang/j1/?PGTID=0d300008-02f1-73d5-3531-e187470fda1e&ClickID=2")
    soup = BeautifulSoup(browser.page_source,"lxml")
    title = soup.find_all("a",class_="strongbox")    
    """for i in title:
        title_list.append(i.string)
    for a in price:
        price_list.append(a.string)
    for num in range(len(title_list)):
        ws.append([title_list[num],price_list[num]])   """ 
    font_data_origin = re.search(r'base64,(.*?)\)', browser.page_source, re.S).group(1)
    font_data_after_decode = base64.b64decode(font_data_origin)
    new_font_name = "font_new.ttf"
    with open(new_font_name, 'wb') as f:
        f.write(font_data_after_decode)
    names =[]
    map_data = tff_parse(new_font_name)
    price = soup.find_all("b",class_="strongbox")
    for a in price:
        names.append(a.string)
    # 有的时候会找不到，可以多执行几次；
    if names:
        for name in names:
            print( 'name in page source', name)
            for j in map_data.keys():
                    name = name.replace(j, map_data[j])
            print( 'name actual', name)

def tff_parse(font_parse_name):
    # 我这里的字体的顺序，如果你的不同，一定要修改
    font_dict = [u' ', u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
    font_base = TTFont('font_base.ttf')
    font_base_order = font_base.getGlyphOrder()[1:]
    # font_base.saveXML('font_base.xml')  调试用

    font_parse = TTFont(font_parse_name)
    # font_parse.saveXML('font_parse_2.xml')调试用
    font_parse_order = font_parse.getGlyphOrder()[1:]

    f_base_flag = []
    for i in font_base_order:
        flags = font_base['glyf'][i].flags
        f_base_flag.append(list(flags))

    f_flag = []
    for i in font_parse_order:
        flags = font_parse['glyf'][i].flags
        f_flag.append(list(flags))

    result_dict = {}
    for a, i in enumerate(f_base_flag):
        for b, j in enumerate(f_flag):
            if comp(i, j):
                key = font_parse_order[b].replace('glyph', '')
                key = eval(r'u"\u' + str(key) + '"').lower()
                result_dict[key] = font_dict[a]
    return result_dict

def comp(L1, L2):

    if len(L1) != len(L2):
        return 0
    for i in range(len(L2)):
        if L1[i] == L2[i]:
            pass
        else:
            return 0
    return 1




if __name__ == '__main__':
    url = "https://su.58.com/qztech/"
    get_data(url)